package com.devouty.mp3online.utils;

import android.os.AsyncTask;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by devouty on 15年7月31日.
 */
public abstract class HttpAsyncRequestTast extends AsyncTask<HttpUriRequest, Integer, String> {
    @Override
    protected String doInBackground(HttpUriRequest... httpUriRequests) {
        LogUtil.log("Work is begin!Running in background.");
        HttpUriRequest request = httpUriRequests[0];
        String result = null;
        HttpClient client = new DefaultHttpClient();
        try{

            HttpResponse response = client.execute(request);
            result = EntityUtils.toString(response.getEntity(),"UTF-8");
        }catch (ClientProtocolException e1)
        {
            e1.printStackTrace();
        }catch (IOException e2)
        {
            e2.printStackTrace();
        }
        return result;
    }

    @Override
    protected void onCancelled() {
        LogUtil.log("Work is interupted");
        super.onCancelled();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        LogUtil.log("Work progress:"+values[0]);
        super.onProgressUpdate(values);
    }
    public abstract void onComplete(String result);
}
